# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.exceptions import ValidationError


class GpInfo(models.Model):
    _name = 'gp.info'
    _description = '省市信息'

    _rec_name = 'province'  # 记录的显示名
    short_description = fields.Char('简短的简介')
    area = fields.Float(string='辖区面积/(平方米)', required=True)
    population_size = fields.Float(string='人口数量', required=True)
    characteristic = fields.Text(string='特色')
    description = fields.Html(string='简介', required=True)

    province = fields.Many2one('gp.province', string='省')
    city = fields.Many2one('gp.city', string='市')
    county = fields.Many2one('gp.county', string='县/区')

    # 约束字段  如某字段的值不能小于0，否则不能创建
    @api.constrains('area')
    def _check_area(self):
        for record in self:
            if record.area <= 0:
                raise ValidationError('辖区面积不能小于0！')

    # 代码层面的约束条件
    @api.multi
    def _checked_area(self):
        if self.population_size <= 0:
            return False
        return True

    _constraints = [
        (_checked_area, '人口不能小于0！', ['population_size'])
    ]
    # _constraints = {约束函数名,约束的异常提示信息,[‘约束的字段’]

    #  截取简介的三十个字符作为列表视图中显示
    @api.onchange('description')
    def get_short_desc(self):
        if self.description:
            self.short_description = self.description[0:30]

    # 实现省市联动，使用此方法 在改变省的时候可以将市和县的值清空
    @api.onchange('province')
    def _onchange_state_id(self):
        # 作用是当改变省时，清空市和区
        self.city = False
        self.county = False
        if self.province:
            return {'domain': {'city': [('province', '=', self.province.id)]}}
        else:
            return {'domain': {'city': []}}

    @api.onchange('city')
    def _onchange_city_id(self):
        self.county = False
        if self.city:
            return {'domain': {'county': [('city', '=', self.city.id)]}}
        else:
            return {'domain': {'county': []}}


class Province(models.Model):
    _name = 'gp.province'
    _description = '省份'

    _rec_name = 'pro_name'
    pro_name = fields.Char(string='省份')
    time = fields.Datetime(string='成立时间')
    city_ids = fields.One2many('gp.city', 'province', string='市')

    # 数据库层面的约束
    _sql_constraints = [('pro_name_uniq', 'unique(pro_name)', '省名必须唯一')]


class City(models.Model):
    _name = 'gp.city'
    _description = '市 信息'

    _rec_name = 'city_name'
    city_name = fields.Char(string='市')
    time = fields.Datetime(string='成立时间')

    province = fields.Many2one('gp.province', string='省份')
    area_ids = fields.One2many('gp.county', 'city', string='县/区')

    # @api.multi
    # def get_province_name(self):
    #     result = []
    #     for city in self:
    #         result.append((city.id, city.province.pro_name))
    #     return result


class County(models.Model):
    _name = 'gp.county'
    _description = '县区信息'

    _rec_name = 'county_name'
    county_name = fields.Char(string='县/区')
    time = fields.Datetime(string='成立时间')

    city = fields.Many2one('gp.city', string='市')

    # @api.multi
    # def get_city_name(self):
    #     result = []
    #     for county in self:
    #         result.append((county.id, county.city.city_name))
